#!/bin/bash

ROOT=`pwd`
ROS_DISTRO=$(grep "^ROS_DISTRO=" config | cut -d'=' -f2)
AGIROS_DISTRO=$(grep "^AGIROS_DISTRO=" config | cut -d'=' -f2)
SRC_TAR_FROM=$(grep "^SRC_TAR_FROM=" config | cut -d'=' -f2)
DEBUG=$(grep "^DEBUG=" config | cut -d'=' -f2)
SRC_TAR_BASE_URL=$(grep "^SRC_TAR_BASE_URL=" config | cut -d'=' -f2)
GITEE_ORG=$(grep "^GITEE_ORG=" config | cut -d'=' -f2)
GITEE_DOMAIN=$(grep "^GITEE_DOMAIN=" config | cut -d'=' -f2)
OBS_DOMAIN=$(grep "^OBS_DOMAIN=" config | cut -d'=' -f2)
OBS_PROJECT=$(grep "^OBS_PROJECT=" config | cut -d'=' -f2)
OPENEULER_BASE_VERSION=$(grep "^OPENEULER_BASE_VERSION=" config | cut -d'=' -f2)
OPENEULER_SP_VERSION=$(grep "^OPENEULER_SP_VERSION=" config | cut -d'=' -f2)

OPENEULER_DEV_BRANCH=${ROS_DISTRO}
OPENEULER_ROS_DEP_NEXT_BRANCH=${OPENEULER_BASE_VERSION}-Next
OPENEULER_NEXT_BRANCH=Multi-Version_ros-${OPENEULER_DEV_BRANCH}_${OPENEULER_BASE_VERSION}-Next
if [ "$OPENEULER_SP_VERSION" == "" ]
then
	OPENEULER_SP_BRANCH=Multi-Version_ros-${OPENEULER_DEV_BRANCH}_${OPENEULER_BASE_VERSION}
	OPENEULER_ROS_DEP_PKG_BRANCH=${OPENEULER_BASE_VERSION}
else
	OPENEULER_SP_BRANCH=Multi-Version_ros-${OPENEULER_DEV_BRANCH}_${OPENEULER_BASE_VERSION}-${OPENEULER_SP_VERSION}
	OPENEULER_ROS_DEP_PKG_BRANCH=${OPENEULER_BASE_VERSION}-${OPENEULER_SP_VERSION}
fi

OUTPUT=${ROOT}/output
ROS_OUTPUT_TMP=${OUTPUT}/.tmp
ROS_SRC_BASE=${OUTPUT}/src
ROS_DEPS_BASE=${OUTPUT}/deps
ROS_REPO_BASE=${OUTPUT}/repo
ROS_BB_BASE=${OUTPUT}/bb
ROS_OBS_BASE=${OUTPUT}/obs
ROS_GITEE_BASE=${OUTPUT}/gitee
LOG=${OUTPUT}/ros-tools.log

ROS_PROJECTS_NAME=${OUTPUT}/ros-projects-name.list
ROS_3RDPARTY_NAME=$PWD/ros-3rdparty.list
ROS_PKG_LIST=${OUTPUT}/ros-pkg.list

mkdir -p ${OUTPUT}
mkdir -p ${ROS_OUTPUT_TMP}

# 统一日志级别定义
LOG_LEVEL_DEBUG=0
LOG_LEVEL_INFO=1
LOG_LEVEL_WARN=2 # Added Warn level explicitly if needed, adjust numbers if logic changes
LOG_LEVEL_ERROR=3 # Adjusted Error level number

# 默认日志级别
LOG_LEVEL=${LOG_LEVEL_INFO}

# 统一日志格式函数
_log() {
    local level_name=$1 # Changed parameter name for clarity
    local msg=$2
    local line_num=${BASH_LINENO[0]}
    local caller=${BASH_SOURCE[1]}
    local log_prefix=""
    local current_level=-1 # Default to invalid level

    # Map level name to numeric level and set prefix
    case $level_name in
        "Error")
            current_level=$LOG_LEVEL_ERROR
            log_prefix="[Error]"
            ;;
        "Warning") # Added Warning case
            current_level=$LOG_LEVEL_WARN
            log_prefix="[Warn ]"
            ;;
        "Info")
            current_level=$LOG_LEVEL_INFO
            log_prefix="[Info ]"
            ;;
        "Debug")
            # Honor both LOG_LEVEL and DEBUG variable for debug messages
            if [ "$DEBUG" != "yes" ]; then
                 return
            fi
            current_level=$LOG_LEVEL_DEBUG
            log_prefix="[Debug]"
            ;;
        *)
            # Handle unknown level if necessary
            echo "`date` [UNKNOWN LEVEL] [${caller}:${line_num}] $msg" >> ${LOG}
            echo "`date` [UNKNOWN LEVEL] [${caller}:${line_num}] $msg"
            return
            ;;
    esac

    # Check against the global LOG_LEVEL setting
    # Log if the message's level is >= the configured LOG_LEVEL
    if [ $current_level -lt $LOG_LEVEL ]; then
         return
    fi

    # 确保日志目录存在
    mkdir -p $(dirname ${LOG})

    # 格式化日志消息，包含行号和调用者信息
    local log_msg="`date` ${log_prefix} [${caller}:${line_num}] $msg"

    # 同时输出到控制台和日志文件
    echo "${log_msg}"
    echo "${log_msg}" >> ${LOG}
}

error_log()
{
    _log "Error" "$*"
}

# info_log now calls _log
info_log()
{
    _log "Info" "$*"
}

# debug_log now only needs to call _log
debug_log()
{
    _log "Debug" "$*"
}

# Add after other log functions
warn_log()
{
    _log "Warning" "$*"
}

if [ "${ROS_DISTRO}" = "" ]
then
        error_log "ROS_DISTRO not defined"
        exit 1
fi
